package com.mojian.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mojian.entity.SysZb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mojian.mapper.SysTbMapper;
import com.mojian.mapper.SysZbMapper; // 引入 SysZbMapper
import com.mojian.entity.SysTb;
import com.mojian.service.SysTbService;
import com.mojian.utils.PageUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;

/**
 * 投标 服务实现类
 */
@Service
@RequiredArgsConstructor
public class SysTbServiceImpl extends ServiceImpl<SysTbMapper, SysTb> implements SysTbService {

    private final SysZbMapper sysZbMapper; // 注入 SysZbMapper

    @Autowired
    private SysTbMapper sysTbMapper;

    /**
     * 查询投标分页列表
     */
    @Override
    public IPage<SysTb> selectPage(SysTb sysTb) {
        LambdaQueryWrapper<SysTb> wrapper = new LambdaQueryWrapper<>();
        // 构建查询条件
        wrapper.eq(sysTb.getId() != null, SysTb::getId, sysTb.getId());
        wrapper.eq(sysTb.getUserId() != null, SysTb::getUserId, sysTb.getUserId());
        wrapper.eq(sysTb.getZbbt() != null, SysTb::getZbbt, sysTb.getZbbt());
        wrapper.eq(sysTb.getZbid() != null, SysTb::getZbid, sysTb.getZbid());
        wrapper.eq(sysTb.getSfje() != null, SysTb::getSfje, sysTb.getSfje());
        wrapper.eq(sysTb.getTbwj() != null, SysTb::getTbwj, sysTb.getTbwj());
        wrapper.eq(sysTb.getStatus() != null, SysTb::getStatus, sysTb.getStatus());
        wrapper.eq(sysTb.getCreateTime() != null, SysTb::getCreateTime, sysTb.getCreateTime());
        wrapper.eq(sysTb.getUpdateTime() != null, SysTb::getUpdateTime, sysTb.getUpdateTime());
        wrapper.eq(sysTb.getA1() != null, SysTb::getA1, sysTb.getA1());
        wrapper.eq(sysTb.getA2() != null, SysTb::getA2, sysTb.getA2());
        wrapper.eq(sysTb.getA3() != null, SysTb::getA3, sysTb.getA3());
        wrapper.eq(sysTb.getA4() != null, SysTb::getA4, sysTb.getA4());
        wrapper.eq(sysTb.getA5() != null, SysTb::getA5, sysTb.getA5());
        wrapper.eq(sysTb.getA6() != null, SysTb::getA6, sysTb.getA6());
        wrapper.eq(sysTb.getA7() != null, SysTb::getA7, sysTb.getA7());
        wrapper.eq(sysTb.getA8() != null, SysTb::getA8, sysTb.getA8());
        wrapper.eq(sysTb.getA9() != null, SysTb::getA9, sysTb.getA9());
        wrapper.eq(sysTb.getA10() != null, SysTb::getA10, sysTb.getA10());
        return page(PageUtil.getPage(), wrapper);
    }
    //投标得分排序
    @Override
    public List<Map<String, Object>> selectCustomList(String zbid) {
        return sysTbMapper.selectCustomList(zbid);
    }
    //中标数据
    @Override
    public List<Map<String, Object>> selectTbUserInfo() {
        return sysTbMapper.selectTbUserInfo();
    }
    //仪表盘数据
    @Override
    public List<Map<String, Object>> selectYBPSJ() {
        return sysTbMapper.selectYBPSJ();
    }


    /**
     * 查询投标列表
     */
    @Override
    public List<SysTb> selectList(SysTb sysTb) {
        LambdaQueryWrapper<SysTb> wrapper = new LambdaQueryWrapper<>();
        // 构建查询条件
        wrapper.eq(sysTb.getId() != null, SysTb::getId, sysTb.getId());
        wrapper.eq(sysTb.getUserId() != null, SysTb::getUserId, sysTb.getUserId());
        wrapper.eq(sysTb.getZbbt() != null, SysTb::getZbbt, sysTb.getZbbt());
        wrapper.eq(sysTb.getZbid() != null, SysTb::getZbid, sysTb.getZbid());
        wrapper.eq(sysTb.getSfje() != null, SysTb::getSfje, sysTb.getSfje());
        wrapper.eq(sysTb.getTbwj() != null, SysTb::getTbwj, sysTb.getTbwj());
        wrapper.eq(sysTb.getStatus() != null, SysTb::getStatus, sysTb.getStatus());
        wrapper.eq(sysTb.getCreateTime() != null, SysTb::getCreateTime, sysTb.getCreateTime());
        wrapper.eq(sysTb.getUpdateTime() != null, SysTb::getUpdateTime, sysTb.getUpdateTime());
        wrapper.eq(sysTb.getA1() != null, SysTb::getA1, sysTb.getA1());
        wrapper.eq(sysTb.getA2() != null, SysTb::getA2, sysTb.getA2());
        wrapper.eq(sysTb.getA3() != null, SysTb::getA3, sysTb.getA3());
        wrapper.eq(sysTb.getA4() != null, SysTb::getA4, sysTb.getA4());
        wrapper.eq(sysTb.getA5() != null, SysTb::getA5, sysTb.getA5());
        wrapper.eq(sysTb.getA6() != null, SysTb::getA6, sysTb.getA6());
        wrapper.eq(sysTb.getA7() != null, SysTb::getA7, sysTb.getA7());
        wrapper.eq(sysTb.getA8() != null, SysTb::getA8, sysTb.getA8());
        wrapper.eq(sysTb.getA9() != null, SysTb::getA9, sysTb.getA9());
        wrapper.eq(sysTb.getA10() != null, SysTb::getA10, sysTb.getA10());
        return list(wrapper);
    }


    /**
     * 查询当前用户投标历史列表
     */
    @Override
    public IPage<SysTb> selectUserTbList(SysTb sysTb) {
        LambdaQueryWrapper<SysTb> wrapper = new LambdaQueryWrapper<>();
        // 构建查询条件
        wrapper.eq(sysTb.getId() != null, SysTb::getId, sysTb.getId());
        wrapper.eq(sysTb.getUserId() != null, SysTb::getUserId, sysTb.getUserId());
        wrapper.eq(sysTb.getZbbt() != null, SysTb::getZbbt, sysTb.getZbbt());
        wrapper.eq(sysTb.getZbid() != null, SysTb::getZbid, sysTb.getZbid());
        wrapper.eq(sysTb.getSfje() != null, SysTb::getSfje, sysTb.getSfje());
        wrapper.eq(sysTb.getTbwj() != null, SysTb::getTbwj, sysTb.getTbwj());
        wrapper.eq(sysTb.getStatus() != null, SysTb::getStatus, sysTb.getStatus());
        wrapper.eq(sysTb.getCreateTime() != null, SysTb::getCreateTime, sysTb.getCreateTime());
        wrapper.eq(sysTb.getUpdateTime() != null, SysTb::getUpdateTime, sysTb.getUpdateTime());
        wrapper.eq(sysTb.getA1() != null, SysTb::getA1, sysTb.getA1());
        wrapper.eq(sysTb.getA2() != null, SysTb::getA2, sysTb.getA2());
        wrapper.eq(sysTb.getA3() != null, SysTb::getA3, sysTb.getA3());
        wrapper.eq(sysTb.getA4() != null, SysTb::getA4, sysTb.getA4());
        wrapper.eq(sysTb.getA5() != null, SysTb::getA5, sysTb.getA5());
        wrapper.eq(sysTb.getA6() != null, SysTb::getA6, sysTb.getA6());
        wrapper.eq(sysTb.getA7() != null, SysTb::getA7, sysTb.getA7());
        wrapper.eq(sysTb.getA8() != null, SysTb::getA8, sysTb.getA8());
        wrapper.eq(sysTb.getA9() != null, SysTb::getA9, sysTb.getA9());
        wrapper.eq(sysTb.getA10() != null, SysTb::getA10, sysTb.getA10());
        wrapper.eq(SysTb::getUserId, StpUtil.getLoginIdAsLong());
        return page(PageUtil.getPage(), wrapper);
    }


    /**
     * 新增投标
     */
    @Override
    public boolean insert(SysTb sysTb) {
        sysTb.setUserId(StpUtil.getLoginIdAsLong());
        return save(sysTb);
    }

    /**
     * 修改投标
     */
    @Override
    public boolean update(SysTb sysTb) {
        return updateById(sysTb);
    }

    /**
     * 定标
     */
    @Override
    public boolean updateDb(SysTb sysTb) {
        updateStatuses(sysTb.getId().toString(),sysTb.getZbid().toString());
        return true;
    }


    /**
     * 批量删除投标
     */
    @Override
    public boolean deleteByIds(List<Long> ids) {
        return removeByIds(ids);
    }


    /**
     * 定标
     */
    public void updateStatuses(String tbId, String zbid) {

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 格式化日期
        String formattedDate = currentDate.format(formatter);

        // 把选中的数据改为定标状态
        UpdateWrapper<SysTb> updateWrapper1 = new UpdateWrapper<>();
        updateWrapper1.eq("id", tbId);
        updateWrapper1.set("status", "1");
        updateWrapper1.set("a1",formattedDate);
        baseMapper.update(null, updateWrapper1);

        // 把未选中的数据改为落选状态
        UpdateWrapper<SysTb> updateWrapper2 = new UpdateWrapper<>();
        updateWrapper2.ne("id", tbId);
        updateWrapper2.eq("zbid", zbid);
        updateWrapper2.set("status", "2");
        baseMapper.update(null, updateWrapper2);

        // 把招标状态改为归档
        UpdateWrapper<SysZb> updateWrapper3 = new UpdateWrapper<>();
        updateWrapper3.eq("id", zbid);
        updateWrapper3.set("status", "2");
        sysZbMapper.update(null, updateWrapper3);
    }
}
